'use strict';

const Service = require('egg').Service;
// 使用
// const moment = require('moment');
class CurdService extends Service {
  // 增加用户的接口
  async add (user) {
    console.log(user);
    const users = await this.app.model.User.create(user);
    return users;
  }
  // eslint-disable-next-line jsdoc/require-param
  /**
  * 申请权限
  * @param {用户信息} user
  * @param {事务} transaction
  */
  async addApplyUser (user, transaction) {
    // console.log(user);
    user.application_time = new Date();
    const users = await this.app.model.User.update(user, {
      where: {
        id: user.id,
      }, transaction,
    });
    return users;
  }
  // 根据id查询用户的接口
  async getDataByPk (id) {
    console.log(id);
    const users = await this.app.model.User.findByPk(id);
    return users;
  }
  // 更新用户信息的接口
  async updateUser (user) {
    // console.log('ddddddddddddd' + new Date());
    user.update_time = new Date();
    // moment(new Date(user.update_time)).utcOffset('+08:00').format('YYYY-MM-DD HH:mm:ss');
    console.log(user);
    const id = user.id;
    // const users = await this.app.model.User.update(user, {
    const users = await this.app.model.User.update(user, {
      where: {
        id,
      },
    });
    return users;
  }
  async update (user, transaction) {
    user.update_time = new Date();
    console.log(user);
    const id = user.id;
    const users = await this.app.model.User.update(user, {
      where: {
        id,
      }, transaction,
    });
    return users;
  }
  // 删除用户信息的接口
  async deleteUser (id) {
    console.log('-----------------------deleteUser----------------------------');
    // console.log(user);
    // const id = user.id;
    const users = await this.app.model.User.destroy({ where: { id } });
    return users;
  }

}

module.exports = CurdService;
